package io.a.sql.smartsense;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.TreeTraverser;

/**
 * 实现TreeTraverser
 */
public class TreeTraversalExample {
    public static void main(String[] args) {
        Node root = new Node("root", ImmutableList.of(
                new Node("child1", ImmutableList.of(
                        new Node("grandchild1", ImmutableList.of()),
                        new Node("grandchild2", ImmutableList.of()))),
                new Node("child2", ImmutableList.of(
                        new Node("grandchild3", ImmutableList.of())))));

        TreeTraverser<Node> treeTraverser = new TreeTraverser<Node>() {
            @Override
            public Iterable<Node> children(Node node) {
                return node.getChildren();
            }
        };

        // 广度优先遍历
        for (Node node : treeTraverser.breadthFirstTraversal(root)) {
            System.out.println(node);
        }

        // 深度优先遍历
        for (Node node : treeTraverser.preOrderTraversal(root)) {
            System.out.println(node);
        }
    }
}
